package cn.hiapi.shop.api;

import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.finance.entity.AmountType;
import cn.hiapi.finance.entity.Recharge;
import cn.hiapi.finance.manage.query.RechargeQuery;
import cn.hiapi.finance.platform.vo.RechargeVo;
import cn.hiapi.finance.service.AmountTypeService;
import cn.hiapi.finance.service.RechargeService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.query.ShopRechargeQuery;
import cn.hiapi.shop.service.shop.ShopLinkUserService;
import cn.hiapi.shopkeeper.consts.AppConst;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author AdinZ
 * @date 2023/3/1 19:19
 */
@RestController("shopkeeperShopRechargeController")
@RequestMapping("/shop/recharge")
public class RechargeController extends BasicQueryController<Recharge, Long, RechargeVo, ShopRechargeQuery> {

    protected RechargeController(RechargeService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }



    @Override
    public List<RechargeVo> toListVo(List<Recharge> list, String[] fields) {
        return toList(list, fields);
    }


    @Override
    public ShopRechargeQuery setQuery(ShopRechargeQuery query) {
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(TokenGet.get().getFid(), query.getShopId(), AppConst.SHOP_ROLE_AMOUNT);
        query.setFid(String.valueOf(query.getShopId()));
        return super.setQuery(query);
    }

    @Override
    public RechargeVo toData(Recharge data, String[] fields) {
        RechargeVo vo = data.copy(new RechargeVo());
        AmountType amountType = this.dispatchContext.getServiceOne(AmountTypeService.class).findByType(data.getMid(), data.getAmountType());
        if (amountType != null)
            vo.setAmountName(amountType.getName());
        return vo;
    }









}
